--[[=========================================================
	WEAPON FIREMODE
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Constructor
--[[-----------------------------------------------------]]--
ObjWeaponFiremode					= class( function(
									self,
									weapon,
									firemode,
									startEnabled,
									holdType,
									canUseIronSights,
									canHolster,
									useRunView,
									canUseMelee,
									useWallView,
									useScope,
									autoReloadOnEmpty,
									canReloadOnlyOnEmpty,
									canInterruptReload )
	
	--	Members
	self.TableName					= "WeaponFiremode"
	self.Weapon						= ValOrDef( weapon, ObjWeapon, ObjWeapon() )
	self.Firemode					= ValOrDef( firemode, ObjFiremode, ObjFiremode() )
	self.StartEnabled				= ValOrDef( startEnabled, "boolean", true )
	self.HoldType					= ValOrDef( holdType, "string", "pistol" )
	self.CanUseIronSights			= ValOrDef( canUseIronSights, "boolean", true )
	self.CanHolster					= ValOrDef( canHolster, "boolean", true )
	self.UseRunView					= ValOrDef( useRunView, "boolean", true )
	self.CanUseMelee				= ValOrDef( canUseMelee, "boolean", true )
	self.UseWallView				= ValOrDef( useWallView, "boolean", true )
	self.UseScope					= ValOrDef( useScope, "boolean", true )
	self.AutoReloadOnEmpty			= ValOrDef( autoReloadOnEmpty, "boolean", true )
	self.CanReloadOnlyOnEmpty		= ValOrDef( canReloadOnlyOnEmpty, "boolean", true )
	self.CanInterruptReload			= ValOrDef( canInterruptReload, "boolean", false )
	
--	SQL management - SERVER only
if SERVER then
	
	--	SQL Insert ( table, ids, contents )
	self.Insert						= function( self )
	
		--	Variables
		local result				= nil
		
		--	Insert the weapon
		self.Weapon:Insert()
		
		--	Insert the firemode
		self.Firemode:Insert()
		
		--	Insert in DB if we got an ID
		if self.Weapon.ID > 0 and self.Firemode.ID > 0 then
			result = sql.Insert(
				self.TableName,
				{ 1, 2 },
				FieldInfo( "Weapon", self.Weapon.ID, "number" ),
				FieldInfo( "Firemode", self.Firemode.ID, "number" ),
				FieldInfo( "StartEnabled", self.StartEnabled, "boolean" ),
				FieldInfo( "HoldType", self.HoldType, "string" ),
				FieldInfo( "CanUseIronSights", self.CanUseIronSights, "boolean" ),
				FieldInfo( "CanHolster", self.CanHolster, "boolean" ),
				FieldInfo( "UseRunView", self.UseRunView, "boolean" ),
				FieldInfo( "CanUseMelee", self.CanUseMelee, "boolean" ),
				FieldInfo( "UseWallView", self.UseWallView, "boolean" ),
				FieldInfo( "UseScope", self.UseScope, "boolean" ),
				FieldInfo( "AutoReloadOnEmpty", self.AutoReloadOnEmpty, "boolean" ),
				FieldInfo( "CanReloadOnlyOnEmpty", self.CanReloadOnlyOnEmpty, "boolean" ),
				FieldInfo( "CanInterruptReload", self.CanInterruptReload, "boolean" )
			)
			
		--	Raise error if we don't have both references
		else
			DEBUG:ErrMsg( "Cannot insert '" .. TableName .. "' without a weapon and a firemode reference", "ObjWeaponFiremode.Insert" )
		end
	end
	
	--	SQL Update ( table, ids, contents )
	self.Update						= function( self )
	
		--	Variables
		local result				= nil
		
		--	Update the weapon
		self.Weapon:Update()
		
		--	Update the firemode
		self.Firemode:Update()
	
		--	Update in DB
		result = sql.Update(
			self.TableName,
			{ 1, 2 },
			FieldInfo( "Weapon", self.Weapon.ID, "number" ),
			FieldInfo( "Firemode", self.Firemode.ID, "number" ),
			FieldInfo( "StartEnabled", self.StartEnabled, "boolean" ),
			FieldInfo( "HoldType", self.HoldType, "string" ),
			FieldInfo( "CanUseIronSights", self.CanUseIronSights, "boolean" ),
			FieldInfo( "CanHolster", self.CanHolster, "boolean" ),
			FieldInfo( "UseRunView", self.UseRunView, "boolean" ),
			FieldInfo( "CanUseMelee", self.CanUseMelee, "boolean" ),
			FieldInfo( "UseWallView", self.UseWallView, "boolean" ),
			FieldInfo( "UseScope", self.UseScope, "boolean" ),
			FieldInfo( "AutoReloadOnEmpty", self.AutoReloadOnEmpty, "boolean" ),
			FieldInfo( "CanReloadOnlyOnEmpty", self.CanReloadOnlyOnEmpty, "boolean" ),
			FieldInfo( "CanInterruptReload", self.CanInterruptReload, "boolean" )
		)
	end
	
	--	SQL Delete ( table, ids, id contents )
	self.Delete						= function( self )
	
		--	Variables
		local result				= nil
	
		--	Delete from DB
		result = sql.Delete(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" )
		)
		
		--	Reset ID if it was deleted
		if result then
			self.ID					= 0
		end
	end
	
	--	SQL Reload ( table, ids, id contents )
	self.Reload						= function( self )
	
		--	Retrieve from DB
		self:FillProperties(
			sql.GetRow(
				self.TableName,
				{ 1, 2 },
				FieldInfo( "Weapon", self.Weapon.ID, "number" ),
				FieldInfo( "Firemode", self.Firemode.ID, "number" ),
				FieldInfo( "StartEnabled", self.StartEnabled, "boolean" ),
				FieldInfo( "HoldType", self.HoldType, "string" ),
				FieldInfo( "CanUseIronSights", self.CanUseIronSights, "boolean" ),
				FieldInfo( "CanHolster", self.CanHolster, "boolean" ),
				FieldInfo( "UseRunView", self.UseRunView, "boolean" ),
				FieldInfo( "CanUseMelee", self.CanUseMelee, "boolean" ),
				FieldInfo( "UseWallView", self.UseWallView, "boolean" ),
				FieldInfo( "UseScope", self.UseScope, "boolean" ),
				FieldInfo( "AutoReloadOnEmpty", self.AutoReloadOnEmpty, "boolean" ),
				FieldInfo( "CanReloadOnlyOnEmpty", self.CanReloadOnlyOnEmpty, "boolean" ),
				FieldInfo( "CanInterruptReload", self.CanInterruptReload, "boolean" )
			)
		)
	end
	
	--	Assign fields from row
	self.FillProperties				= function( self, row )
		self.Weapon:Reload( tonumber( row["Weapon"] ) )
		self.Firemode:Reload( tonumber( row["Firemode"] ) )
		self.StartEnabled			= tobool( row["StartEnabled"] )
		self.HoldType				= tostring( row["HoldType"] )
		self.CanUseIronSights		= tobool( row["CanUseIronSights"] )
		self.CanHolster				= tobool( row["CanHolster"] )
		self.UseRunView				= tobool( row["UseRunView"] )
		self.CanUseMelee			= tobool( row["CanUseMelee"] )
		self.UseWallView			= tobool( row["UseWallView"] )
		self.UseScope				= tobool( row["UseScope"] )
		self.AutoReloadOnEmpty		= tobool( row["AutoReloadOnEmpty"] )
		self.CanReloadOnlyOnEmpty	= tobool( row["CanReloadOnlyOnEmpty"] )
		self.CanInterruptReload		= tobool( row["CanInterruptReload"] )
	end
	
end	--	End of SQL management

--	Other server functions
if SERVER then

	--	Returns a table for transfers to clients
	self.GetClientTable				= function( self )
		return {
			["Weapon"]				= self.Weapon:GetClientTable(),
			["Firemode"]			= self.Firemode:GetClientTable(),
			["StartEnabled"]		= self.StartEnabled,
			["HoldType"]			= self.HoldType,
			["CanUseIronSights"]	= self.CanUseIronSights,
			["CanHolster"]			= self.CanHolster,
			["UseRunView"]			= self.UseRunView,
			["CanUseMelee"]			= self.CanUseMelee,
			["UseWallView"]			= self.UseWallView,
			["UseScope"]			= self.UseScope,
			["AutoReloadOnEmpty"]	= self.AutoReloadOnEmpty,
			["CanReloadOnlyOnEmpty"]= self.CanReloadOnlyOnEmpty,
			["CanInterruptReload"]	= self.CanInterruptReload
		}
	end

end
	
end)